package client.ui.view.panel.good;

import client.UserState;
import client.control.Controller;
import client.control.ResponseEntity;
import client.entity.Good;
import client.ui.component.button.MaterialButton;
import client.ui.component.edittext.InputArea;
import client.ui.component.edittext.TextEdit;
import client.ui.theme.ThisFont;
import client.ui.util.MaterialColors;
import client.ui.view.base.PanelControl;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

public class InputCommentPanel extends JPanel implements PanelControl {
    Executor singleThreadExecutor = Executors.newSingleThreadExecutor();
    private Good good;
    private TextEdit starEdit = new TextEdit("评价星级(数字1~5)");
    private JPanel commentPanel;
    private InputArea inputArea;
    public InputCommentPanel(JPanel commentPanel, Good good) {
        super();
        this.commentPanel = commentPanel;
        initial();
    }

    private void initial(){
        this.setLayout(new BorderLayout());
        this.setBackground(MaterialColors.WHITE);
        JButton send = MaterialButton.BlueButton("评论",null);

        inputArea = new InputArea(null,send,null);
        inputArea.setInputAreaFont(ThisFont.MIDDLE_FONT);
        JLabel text = new JLabel("编辑评论   ");
        starEdit.setBackground(MaterialColors.WHITE);
        Box box = Box.createHorizontalBox();
        box.add(text);
        box.add(starEdit);

        text.setBorder(new EmptyBorder(4,12,0,12));
        text.setFont(ThisFont.MIDDLE_FONT);
        this.add(box, BorderLayout.NORTH);
        this.add(inputArea, BorderLayout.CENTER);

        send.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try{
                    int code = Integer.parseInt(starEdit.getText());
                    if (code < 1 || code > 5){
                        throw new Exception();
                    }
                } catch (Exception ignore){
                    starEdit.setErrorText("星级评价不合法");
                    return;
                }
                starEdit.setErrorText("");
                singleThreadExecutor.execute(new Runnable() {
                    @Override
                    public void run() {
                        ResponseEntity responseEntity = Controller.COMMENT_CONTROL.addComment(
                                UserState.getToken(), 0, good.getGoodId(), inputArea.getText());
                        if (responseEntity.getCode() == 0){
                            SwingUtilities.invokeLater(new Runnable() {
                                @Override
                                public void run() {
                                    inputArea.setText("");
                                    if (commentPanel instanceof PanelControl){
                                        ((PanelControl) commentPanel).updatePanel(good);
                                    }
                                }
                            });
                        }
                        Controller.EVALUATION_CONTROL.addEvaluation(
                                UserState.getToken(), good.getGoodId(), Integer.parseInt(starEdit.getText()));
                    }
                });
            }
        });
    }

    @Override
    public void updatePanel(Object object) {
        if (object instanceof Good){
            this.good = (Good) object;
        }
    }
}
